package com.hudson.store.index;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class IndexHeader {
    private static final int startTimeStampIndex = 0;
    private static final int endTimeStampIndex = 8;
    private static final int startOffsetLimitIndex = 16;
    private static final int endOffsetLimitIndex = 24;
    private static final int hashSlotCountIndex = 32;
    private static final int indexCountIndex = 36;

//    private static long startTimeStamp;//开始存储的时间戳
//    private static long endTimeStamp;//最新存储的时间戳
//    private static long startOffsetLimit;//开始存储的offset
//    private static long endOffsetLimit;//最新存储的offset
//    private static int hashSlotCount;//hash槽位大小
//    private static int indexCount;//存储索引的最大数量


    private final AtomicLong startTimeStampAtomic = new AtomicLong(0);
    private final AtomicLong endTimeStampAtomic = new AtomicLong(0);
    private final AtomicLong startOffsetLimitAtomic = new AtomicLong(0);
    private final AtomicLong endOffsetLimitAtomic = new AtomicLong(0);
    private final AtomicInteger hashSlotCountAtomic = new AtomicInteger(0);
    private final AtomicInteger indexCountAtomic = new AtomicInteger(1);

    private static ByteBuffer buffer;

    public IndexHeader(ByteBuffer byteBuffer) {
        buffer = byteBuffer;
        load();
    }

    public void load() {
        this.startTimeStampAtomic.set(buffer.getLong(startTimeStampIndex));
        this.endTimeStampAtomic.set(buffer.getLong(endTimeStampIndex));
        this.startOffsetLimitAtomic.set(buffer.getLong(startOffsetLimitIndex));
        this.endOffsetLimitAtomic.set(buffer.getLong(endOffsetLimitIndex));
        this.hashSlotCountAtomic.set(buffer.getInt(hashSlotCountIndex));
        this.indexCountAtomic.set(buffer.getInt(indexCountIndex));

        if (this.indexCountAtomic.get() <= 0) {
            this.indexCountAtomic.set(1);  // 确保至少为1
        }
    }


    public void flush() {
        buffer.putLong(startTimeStampIndex, startTimeStampAtomic.get());
        buffer.putLong(endTimeStampIndex, endTimeStampAtomic.get());
        buffer.putLong(startOffsetLimitIndex, startOffsetLimitAtomic.get());
        buffer.putLong(endOffsetLimitIndex, endOffsetLimitAtomic.get());
        buffer.putInt(hashSlotCountIndex, hashSlotCountAtomic.get());
        buffer.putInt(indexCountIndex, indexCountAtomic.get());
    }

    public long getStartTimeStampAtomic() {
        return startTimeStampAtomic.get();
    }

    public long getEndTimeStampAtomic() {
        return endTimeStampAtomic.get();
    }

    public long getStartOffsetLimitAtomic() {
        return startOffsetLimitAtomic.get();
    }

    public long getEndOffsetLimitAtomic() {
        return endOffsetLimitAtomic.get();
    }

    public int getHashSlotCountAtomic() {
        return hashSlotCountAtomic.get();
    }

    public int getIndexCountAtomic() {
        return indexCountAtomic.get();
    }

    public void setStartTimeStampAtomic(long startTimeStamp) {
        startTimeStampAtomic.set(startTimeStamp);
    }

    public void updateEndTimeStampAtomic(long endTimeStamp) {
        endTimeStampAtomic.set(endTimeStamp);
    }

    public void setStartOffsetLimitAtomic(long startOffset) {
        startOffsetLimitAtomic.set(startOffset);
    }

    public void updateEndOffsetLimitAtomic(long endOffset) {
        endOffsetLimitAtomic.set(endOffset);
    }

    public void incHashSlotCountAtomic() {
        hashSlotCountAtomic.getAndIncrement();
    }

    public void incIndexCountAtomic() {
        indexCountAtomic.getAndIncrement();
    }
}
